<template>
  <div>
    <div class="forgot-panel">
      <el-form class="forgot-form" ref="form" :model="form" :rules="formRules" label-width="0px">
        <el-form-item prop="username">
          <el-input type="text" v-model="form.username" :placeholder="$root.lang.forgotStep1.username"></el-input>
        </el-form-item>
        <el-form-item prop="captcha" class="form-captcha" align="left">
          <el-input type="text" v-model="form.captcha" @keyup.enter.native="onSubmit" :placeholder="$root.lang.forgotStep1.captcha"></el-input>
          <img class="pull-right" ref="captcha" @click="getUserCode()" :src="picUrl" :title="$root.lang.forgotStep1.captchaTip"/>
          <p class="form-item-tip">{{ $root.lang.userinfo.captchaTip }}</p>
        </el-form-item>
        <el-form-item>
          <p class="form-error">{{ errmsg }}</p>
          <el-button class="forgot-btn" type="primary" @click="onSubmit" :loading="loading">{{ $root.lang.forgotStep1.btn }}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      form: this.content,
      picUrl: '',
      formRules: {
        username: [{ required: true, message: this.$root.lang.forgotStep1.rule.account, trigger: 'blur' }],
        captcha: [{ required: true, message: this.$root.lang.forgotStep1.rule.captcha, trigger: 'blur' }]
      },
      errmsg: ''
    }
  },
  props: {
    content: {
      type: Object,
      default: {
        username: '',
        captcha: ''
      }
    }
  },
  methods: {
  	onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.checkStep1()
        } else {
          return false
        }
      })
  	},
    checkStep1() {
      this.loading = true
      this.request({
        url: '/api/user/forget/password/check/account',
        method: 'post',
        data: {
          account: this.form.username,
          captcha: this.form.captcha
        }
      }).then(response => {
        this.loading = false
        this.$emit('receive',{
          key: 'activeIndex',
          value: 2
        })
        this.$emit('receive',{
          key: 'form',
          value: this.form
        })
        this.$emit('receiveMsg',response.result)
      }).catch(error => {
        this.getUserCode()
        this.loading = false
        this.errmsg = error.errmsg
      })
    },
    getUserCode() {
      const suffix = process.env.API_1 === '/' ? '' : process.env.API_1;
      this.picUrl= suffix +'/api/user/captcha/forget/password?'+Math.random()
    }
  },
  created() {
    this.getUserCode()
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import '@/css/style.scss';
.forgot-panel {
  position: relative;
  margin: 30px auto 0;
  padding: 100px 50px;
  width: 600px;
  background-color: #ffffff;
}
.forgot-form {
  margin: 0 auto;
  width: 60%;
}
.el-form-item {
  margin-bottom: 20px;
}
.form-captcha {
  .el-input{
    display: inline-block;
    width: 60%;
  }
  img {
    height: 40px;
    border-radius: 4px;
    cursor: pointer;
  }
}
.forgot-btn {
  width: 100%;
}
</style>
